package com.goods.business.service.imp;

import com.goods.business.mapper.*;
import com.goods.business.service.InStockService;
import com.goods.common.model.business.*;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.InStockDetailVO;
import com.goods.common.vo.business.InStockItemVO;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.business.SupplierVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;

@Service
public class InStockServiceImpl implements InStockService {

    @Resource
    private InStockMapper inStockMapper;
    @Resource
    private InStockInfoMapper inStockInfoMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private SupplierMapper supplierMapper;
    @Resource
    private ProductStockMapper productStockMapper;

    @Override
    public Map findInStockList(Integer pageNum, Integer pageSize, Integer status, Integer type, String inNum, String startTime, String endTime) {
        //查询所有数据
        //List<InStock> inStockList = inStockMapper.findInStockList(status,type,inNum,startTime,endTime);
        Example example = new Example(InStock.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status",status);
        if (!StringUtils.isEmpty(type)){
            criteria.andEqualTo("type",type);
        }
        if (!StringUtils.isEmpty(inNum)){
            criteria.andEqualTo("inNum",inNum);
        }
        if (!StringUtils.isEmpty(startTime) && !StringUtils.isEmpty(endTime)){
            criteria.andBetween("createTime",startTime,endTime);
        }
        List<InStock> inStockList = inStockMapper.selectByExample(example);

        //转Vo
        List<InStockVO> inStockVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inStockList)){
            for (InStock inStock : inStockList) {
                Long supplierId = inStock.getSupplierId();
                Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
                InStockVO inStockVO = new InStockVO();
                BeanUtils.copyProperties(inStock,inStockVO);
                inStockVO.setPhone(supplier.getPhone());
                inStockVOList.add(inStockVO);
            }
        }
        //分页
        List<InStockVO> page = ListPageUtils.page(inStockVOList, pageSize, pageNum);
        int total = inStockVOList.size();
        Map map = new HashMap();
        map.put("rows",page);
        map.put("total",total);
        return map;
    }

    @Override
    public InStockDetailVO detail(Long id) {
        //                    this.detailTable = res.data.itemVOS;
        //                    this.detailTotal = res.data.total;
        //                    this.supplier=res.data.supplierVO;
        //                    this.pStatus=res.data.status;
        InStockDetailVO inStockDetailVO = new InStockDetailVO();
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        String inNum = inStock.getInNum();
        List<InStockInfo> inStockInfoList = inStockInfoMapper.selectByInNum(inNum);
        //查询product封装成InStockItemVO集合
        List<InStockItemVO> inStockItemVOList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(inStockInfoList)){
            for (InStockInfo inStockInfo : inStockInfoList) {
                InStockItemVO inStockItemVO = new InStockItemVO();
                String pNum = inStockInfo.getPNum();
                Product product = productMapper.selectBypNum(pNum);
                BeanUtils.copyProperties(product,inStockItemVO);
                String num = product.getPNum();
                ProductStock productStock = productStockMapper.selectByPNum(num);
                int i = Integer.parseInt(String.valueOf(productStock.getStock()));
                inStockItemVO.setCount(i);
                inStockItemVOList.add(inStockItemVO);
            }
        }

        //查询supplierVO
        Long supplierId = inStock.getSupplierId();
        Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(supplier,supplierVO);

        //封装InStockDetailVO对象

        inStockDetailVO.setInNum(inStock.getInNum());
        inStockDetailVO.setStatus(inStock.getStatus());
        inStockDetailVO.setType(inStock.getType());
        inStockDetailVO.setOperator(inStock.getOperator());
        inStockDetailVO.setSupplierVO(supplierVO);
        inStockDetailVO.setTotal(inStockItemVOList.size());
        inStockDetailVO.setItemVOS(inStockItemVOList);

        return inStockDetailVO;
    }

    @Override
    public int remove(Long id) {
        int status = 1;
        int i = inStockMapper.updateByStatus(status,id);
        return i;
    }

    @Override
    public int back(Long id) {
        int status = 0;
        int i = inStockMapper.updateByStatus(status,id);
        return i;
    }

    @Override
    public int delete(Long id) {
        int i = inStockMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public void addIntoStock(Map map) {
        InStock inStock = new InStock();
        Supplier supplier = new Supplier();
        String s = UUID.randomUUID().toString();
        int productNum = 0;
        inStock.setInNum(s);
        BeanUtils.copyProperties(map,inStock);
        if (!map.containsKey("supplierId")){//判断map里是否存在“”key值
            //BeanUtils.copyProperties(map,supplier);
            supplier.setAddress((String) map.get("address"));
            supplier.setContact((String) map.get("contact"));
            supplier.setName((String) map.get("name"));
            supplier.setEmail((String) map.get("email"));
            supplier.setPhone((String) map.get("phone"));
            supplier.setSort((Integer) map.get("sort"));
            supplier.setCreateTime(new Date());
            supplier.setModifiedTime(new Date());
            supplierMapper.insert(supplier);
            inStock.setSupplierId(supplier.getId());
            //return;
        }else {
            Long supplierId = Long.valueOf(String.valueOf(map.get("supplierId")));
            inStock.setSupplierId(supplierId);
        }
        List<Map> inStockInfoList = (List<Map>) map.get("products");
        for (Map inStockInfo : inStockInfoList) {
            InStockInfo inStockInfo1 = new InStockInfo();
            Product product = productMapper.selectByPrimaryKey(inStockInfo.get("productId"));
            inStockInfo1.setInNum(s);
            inStockInfo1.setPNum(product.getPNum());
            inStockInfo1.setProductNumber((Integer) inStockInfo.get("productNumber"));
            inStockInfo1.setCreateTime(new Date());
            inStockInfo1.setModifiedTime(new Date());
            inStockInfoMapper.insert(inStockInfo1);
            productNum+=inStockInfo1.getProductNumber();
        }
        inStock.setType((Integer) map.get("type"));
        inStock.setRemark((String) map.get("remark"));
        inStock.setCreateTime(new Date());
        inStock.setModified(new Date());
        inStock.setStatus(2);
        inStock.setProductNumber(productNum);
        inStock.setOperator("admin");
        inStockMapper.insert(inStock);

    }

    @Override
    public int publish(Long id) {
        int status = 0;
        int i = inStockMapper.updateByStatus(status,id);
        return i;

    }

}
